<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { ToastError, ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  const formLabelWidth = '160px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:''
  })
  interface formQuert {
    pool_id:number
    min_num:number
    max_num:number
    pool_name:string
    gift_price:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    pool_name: [{ required: true, message: "请输入礼物池名称", trigger: "blur" }],
    min_num: [{ required: true, message: "请输入最小消耗值", trigger: "blur" }],
    max_num: [{ required: true, message: "请输入最大消耗值", trigger: "blur" }],
    gift_price: [{ required: true, message: "请输入礼物价格", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const { pool_id, min_num=0, max_num=0 } = form
      if(!min_num) return ToastError('请输入最小消耗值')
      if(!max_num) return ToastError('请输入最大消耗值')
      const loading = Toastloading()
      let submitSet = false
      if(pool_id) {
        await propApi.luckGiftEditPool(form).then(()=>{
          ToastSuccess('编辑礼物池成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }else {
        await propApi.luckGiftAddPool(form).then(()=>{
          ToastSuccess('新增礼物池成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = (query:any={})=> {
    const { id, min_num='', max_num='', pool_name='', gift_price='' } = query
    Object.assign(form,{
      min_num,
      max_num,
      pool_name,
      gift_price
    })
    if(id) form.pool_id = id
    action.title = `${id?`编辑${pool_name}`:'新增'}礼物池`
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="40%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" prop="pool_name" label="礼物池名称">
          <el-input v-model="form.pool_name" placeholder="请输入礼物池名称" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" prop="gift_price" label="礼物价格">
          <el-input @input="allNumber(form,'gift_price')" v-model="form.gift_price" placeholder="请输入礼物价格" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" prop="min_num" label="最小消耗值">
          <el-input @input="allNumber(form,'min_num')" v-model="form.min_num" placeholder="请输入最小消耗值" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" prop="max_num" label="最大消耗值">
          <el-input @input="allNumber(form,'max_num')" v-model="form.max_num" placeholder="请输入最大消耗值" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
